package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import po.ComboboxItem;
import po.Memory;
import po.Result;
import service.MemoryService;

import java.util.List;

@Controller
@RequestMapping("/memory")
public class MemoryController {
    @Autowired
    private MemoryService memoryService;

    @RequestMapping("/getMemoryInfoView")
    public String getMemoryInfoView() {
        return "resource/memoryInfo";
    }

    @RequestMapping("/findAllMemories")
    public @ResponseBody
    List<Memory> findAllMemories(Memory memory) throws Exception {
        return memoryService.findAllMemories(memory);
    }

    @RequestMapping("/getAddMemoryView")
    public String getAddMemoryView() {
        return "resource/addMemory";
    }

    @RequestMapping("/addMemory")
    public @ResponseBody
    Result addMemory(Memory memory) {
        return memoryService.addMemory(memory);
    }

    @RequestMapping("/deleteMemoryById")
    public @ResponseBody
    Result deleteMemoryById(int id) {
        return memoryService.deleteMemoryById(id);
    }

    @RequestMapping("/findMemoryForComb")
    public @ResponseBody
    List<ComboboxItem> findMemoryForComb() throws Exception {
        return memoryService.findMemoryForComb();
    }

    @RequestMapping("/getEditMemoryView")
    public String getEditMemoryView() {
        return "resource/editMemory";
    }

    @RequestMapping("/updateMemory")
    public @ResponseBody
    Result updateMemory(Memory memory) {
        return memoryService.updateMemoryById(memory.getId(), memory);
    }
}
